#include "GBForm.h"
#include "GBLabel.h"
#include <windows.h>
#include <gl.h>

GBForm::GBForm() : m_bIsFocused(true), m_bIsActive(false)
{
	this->m_pParent = NULL;	// I am my own parent
}

GBForm::~GBForm()
{

}

void GBForm::RegisterEvents(bool bIsActive)
{

}

void GBForm::Update(const float fTimeStep)
{
	// Update myself then update the children
	GBWidget::Update(fTimeStep);

	// Update children
	unsigned unSize = (unsigned)m_children.size();
	for (unsigned i = 0; i < unSize; i++)
		m_children[i]->Update(fTimeStep);
}

void GBForm::Render()
{
	// Update form position based on whatever alignment it is
	if(m_AlignType != ALIGN_LEFT)
	{
		Vec4 scale = Diag(m_local);

		// This is here because we don't want to change the form's position every frame
		// Otherwise it would look like the form would be moving @ultraspeed!
		if(m_AlignType == ALIGN_RIGHT)
		{
			static float oldX = m_local.wAxis().X() - scale.X();

			if(oldX != m_local.wAxis().X())
				oldX = m_local.wAxis().X() - scale.X();

			m_local.wAxis().X() = oldX;
		}
		else if(m_AlignType == ALIGN_CENTER)
		{
			static float oldX = m_local.wAxis().X() - (scale.X() * 0.5f);

			if(oldX != m_local.wAxis().X())
				oldX = m_local.wAxis().X() - (scale.X() * 0.5f);

			m_local.wAxis().X() = oldX;
		}
	}

	glEnable(GL_TEXTURE_2D);

	// Render Children
	unsigned numChildren = (unsigned)m_children.size();
	for (unsigned i = 0; i < numChildren; i++)
	{
		if(!m_children[i]->IsVisible()) continue;

		m_children[i]->Render();
	}

	glDisable(GL_TEXTURE_2D);
}

void GBForm::RemoveWidget(GBWidget* pWidget)
{
	if(pWidget == NULL) return;

	std::vector<GBWidget*>::iterator pIter;

	// Iterate thru and check memory addresses
	for (pIter = m_children.begin(); pIter != m_children.end(); pIter++)
	{
		if((*pIter) == pWidget)
		{
			m_children.erase(pIter);
			break;
		}
	}
}

void GBForm::RemoveWidgetAll()
{
	m_children.clear();
}

void GBForm::AddWidget(GBWidget* pWidget)
{
	// This essentially is a child in the tree
	if(pWidget == NULL) return;

	std::vector<GBWidget*>::const_iterator pIter;

	// Don't push back the same address check, there can be ONLY one
	for (pIter = m_children.begin(); pIter != m_children.end(); pIter++)
		if((*pIter) == pWidget) return;

	// Okay so now that you're my son im making your position relative to mine!
	//pWidget->SetPosition(GetPosition() + pWidget->GetPosition());

	// This form is the parent
	pWidget->m_pParent = this;

	m_children.push_back(pWidget);
}

void GBForm::HandleEvent(Event* pEvent)
{
	if(!m_bIsActive) return;

	// NOTE: This feature is currently not implemented (JB)
	// TODO: (OLD)
	WidgetEvent _event;

	if(pEvent->GetEventID() == "Input_Left_Click")
		_event = WIDGET_EVENT_CLICK;
	else if(pEvent->GetEventID() == "Input_Dbl_Left_Click")
		_event = WIDGET_EVENT_DBL_CLICK;
	// 

	// Go through all the children's events...
	unsigned unSize = (unsigned)m_children.size();
	for (unsigned i = 0; i < unSize; i++)
	{
		if(m_children[i]->m_pFunctions[_event])
			m_children[i]->m_pFunctions[_event]();
	}
}

void GBForm::Copy(const GBWidget* pRef)
{
	// Copy over the old window stuff first
	GBWidget::Copy(pRef);

	GBForm* pForm = (GBForm*)pRef;

	this->m_bIsFocused = pForm->m_bIsFocused;
	this->m_bIsActive = pForm->m_bIsActive;

	// Now call copy on all the children (oh boy!)
	unsigned unSize = (unsigned)pForm->m_children.size();

	m_children = pForm->m_children;
}